const int MAX_BULLETS = 100;
const int WIDTH = 1024;
const int HEIGHT = 768;
enum poweruptype{UNLIMAMMO, INVINCIBILITY, HEALTHBOOST, NOPOWER};
struct Player
{
        float health;
        int live;
        int score;
        int x;
        int y;
		int powerup;
		int powerupcountdown;
		int gundelay;
		int setdelay;
		int gunheat;
};
void playerinit(Player &player)
{
        player.health = 50;
        player.live = true;
        player.score = 0;
        player.x = WIDTH/2;
        player.y = HEIGHT/2;
		player.powerup = NOPOWER;
		player.powerupcountdown = 2000;
		player.gundelay = 0;
		player.setdelay = 65;
		player.gunheat = 0;
}
void playermove(Player &player, int x, int y)
{

        player.x = player.x + x;
        player.y = player.y + y;
}
void playerdraw(Player &player, ALLEGRO_BITMAP *play)
{
	al_draw_bitmap(play,player.x-16,player.y-16,0);
}

struct Mouse
{
        int x;
        int y;
        bool rightclick;
                bool leftclick;
};
void initmouse(Mouse &mouse)
{
        mouse.x = 0;
        mouse.y = 0;
        mouse.rightclick = false;
        mouse.leftclick = false;
}

struct Evilrobots
{
        float x;
        float y;
        bool live;
        int health;
		float directionx;
        float directiony;
};
void initevilrobots(Evilrobots evilrobots[])
{
        for(int i = 0;i < 100; i++)
        {
            evilrobots[i].health = 100;
            evilrobots[i].live = false;
            evilrobots[i].x = 10;
            evilrobots[i].y = 10;
            evilrobots[i].directionx = 1;
            evilrobots[i].directionx = 1;
        }
}
void createevilrobots(Evilrobots evilrobots[], Player &player)
{
        for(int i = 0;i < 100; i++)
        {
                if(evilrobots[i].live == false)
                {
                        evilrobots[i].health = 100;
                        evilrobots[i].live = true;
                        int test = rand() % 4+1;
						switch(test)
						{
							case 1:
								evilrobots[i].x = 0;
								evilrobots[i].y = 0;
								break;
							case 2:
								evilrobots[i].x = 1024;
								evilrobots[i].y = 0;
								break;
							case 3:
								evilrobots[i].x = 1024;
								evilrobots[i].y = 768;
								break;
							case 4:
								evilrobots[i].x = 0;
								evilrobots[i].y = 768;
								break;
						}
                        evilrobots[i].directionx = evilrobots[i].x - player.x;
                        evilrobots[i].directiony = evilrobots[i].y - player.y;
                        return;
                }
        }
}
void drawevilrobots(Evilrobots evilrobots[], ALLEGRO_BITMAP *play)
{
        for(int i = 0;i < 100; i++)
        {
                if(evilrobots[i].live == true)
                {       
                        	al_draw_bitmap(play,evilrobots[i].x-16,evilrobots[i].y-16,0);
                }
        }
}
void EvilrobotsandEvilrobots(Evilrobots evilrobots[], Evilrobots evilrobot[])
{
	for(int i = 0; i < 100; i++)
	{
		if(evilrobots[i].live == true)
		{
			for(int c = 0; c < 100; c++)
			{
				if(evilrobot[c].live == true)
				{
					if(c != i)
					{
                        float distance = sqrt(pow(float(evilrobots[i].x - evilrobot[c].x), 2) + pow(float(evilrobots[i].y  - evilrobot[c].y), 2));

                        if(distance < 40)
						{
							float testdx = evilrobot[c].x - evilrobots[i].x;
							float testdy = evilrobot[c].y - evilrobots[i].y;

							float vectorLength = sqrt(testdx * testdx + testdy * testdy);
							float dx = testdx / vectorLength;
							float dy = testdy / vectorLength;

							evilrobot[c].x = evilrobot[c].x + dx/2;
							evilrobot[c].y = evilrobot[c].y + dy/2;

							evilrobots[i].x = evilrobots[i].x - dx/2;
							evilrobots[i].y = evilrobots[i].y - dy/2;
						}
					}
				}
			}
		}
	}
}
void updateevilrobots(Evilrobots evilrobots[], Player player)
{
        for(int i = 0;i < 100; i++)
        {
                if(evilrobots[i].live)
                {       
					evilrobots[i].directionx = evilrobots[i].x - player.x;
					evilrobots[i].directiony = evilrobots[i].y - player.y;

					float vectorLength = sqrt(evilrobots[i].directionx * evilrobots[i].directionx + evilrobots[i].directiony * evilrobots[i].directiony);
					float dx = evilrobots[i].directionx / vectorLength;
					float dy = evilrobots[i].directiony / vectorLength;

					evilrobots[i].x = evilrobots[i].x - dx/2;
					evilrobots[i].y = evilrobots[i].y - dy/2;
                }
        }
}

struct Bullet
{
        float x;
        float y;
        float directionx;
        float directiony;
        bool live;
};
void bulletinit(Bullet bullet[])
{
        for(int i = 0; i < 100; i++)
        {
                bullet[i].x = 0;
                bullet[i].y = 0;
                bullet[i].directionx = 0;
                bullet[i].directiony = 0;
                bullet[i].live = false;
        }
}
void createbullet(Bullet bullet[], Player &player, Mouse mouse)
{
        for(int i = 0; i < 100; i++)
        {
                if(bullet[i].live == false)
                {
                        bullet[i].x = player.x;
                        bullet[i].y = player.y;
                                                bullet[i].directionx = mouse.x - bullet[i].x;
                                                bullet[i].directiony = mouse.y - bullet[i].y;
                        bullet[i].live = true;
                        return;
                }
        }
}
void bulletupdate(Bullet bullet[])
{
        for(int i = 0; i < 100; i++)
        {
                if(bullet[i].live)
                {
					float vectorLength = sqrt(bullet[i].directionx*bullet[i].directionx + bullet[i].directiony*bullet[i].directiony);
					float dx = bullet[i].directionx / vectorLength;
					float dy = bullet[i].directiony / vectorLength;

					bullet[i].x = bullet[i].x + dx*2;
					bullet[i].y = bullet[i].y + dy*2;
					if(bullet[i].x > 1024)
						bullet[i].live = false;
					if(bullet[i].x < 0)
						bullet[i].live = false;
					if(bullet[i].y < 0)
						bullet[i].live = false;
					if(bullet[i].y > 768)
						bullet[i].live = false;
                }
        }
}
void bulletdraw(Bullet bullet[])
{
        for(int i = 0; i < 100; i++)
        {
                if(bullet[i].live)
                        al_draw_filled_circle(bullet[i].x,bullet[i].y,5,al_map_rgb(10,10,200));
        }
}

struct Frisbee
{
        int x;
        int y;
		int targety;
        bool live;
		bool inair;
};
void frisbeeinit(Frisbee frisbee[])
{
        for(int i = 0; i < 5; i++)
        {
                frisbee[i].x = 0;
                frisbee[i].y = 0;
                frisbee[i].live = false;
				frisbee[i].inair = true;
        }
}
void frisbeecreate(Frisbee frisbee[])
{
        for(int i = 0; i < 5; i++)
        {
                if(frisbee[i].live == false)
                {
                        frisbee[i].live = true;
                        frisbee[i].x = (rand() % 994)+15;
                        frisbee[i].targety = (rand() % 738)+15;
						frisbee[i].y = 0;
						frisbee[i].inair = true;
                        return;
                }
        }
}
void frisbeeupdate(Frisbee frisbee[])
{
	for(int i = 0; i < 5; i++)
	{
		if(frisbee[i].inair)
		{
			frisbee[i].y++;
			if(frisbee[i].targety == frisbee[i].y)
			{
				frisbee[i].inair = false;
			}
		}
	}
}
void frisbeedraw(Frisbee frisbee[])
{
        for(int i = 0; i < 5; i++)
        {
                if(frisbee[i].live)
                {
                        al_draw_filled_circle(frisbee[i].x,frisbee[i].y,15,al_map_rgb(180,10,10));
                }
        }
}

struct Powerups
{
	float x;
	float y;
	bool live;
	int type;
};
void initpowerups(Powerups power[])
{
        for(int i = 0; i < 5; i++)
        {
                power[i].x = 0;
                power[i].y = 0;
                power[i].live = false;
        }
}
void powerupscreate(Powerups power[])
{
        for(int i = 0; i < 5; i++)
        {
                if(power[i].live == false)
                {
                        power[i].live = true;
                        power[i].x = (rand() % 994)+15;
                        power[i].y = (rand() % 738)+15;
						int test = rand() % 3;
						power[i].type = test;
						std::cout << test << '\n';
                        return;
                }
        }
}
void powerupdraw(Powerups power[], ALLEGRO_BITMAP *ammo,ALLEGRO_BITMAP *invinc,ALLEGRO_BITMAP *health)
{
        for(int i = 0; i < 5; i++)
        {
                if(power[i].live)
                {
					switch (power[i].type)
					{
						case UNLIMAMMO:
							al_draw_bitmap(ammo,power[i].x,power[i].y,NULL);
							break;
						case INVINCIBILITY:
							al_draw_bitmap(invinc,power[i].x,power[i].y,NULL);
							break;
						case HEALTHBOOST:
							al_draw_bitmap(health,power[i].x,power[i].y,NULL);
							break;
					}
                }
        }
}
void powerupdate(Player &player)
{
	switch(player.powerup)
	{
		case UNLIMAMMO:
			if(player.powerupcountdown > 0)
			{
				player.powerupcountdown--;
				player.gunheat = 0;
			}
			else
			{
				player.powerup = NOPOWER;
				player.powerupcountdown = 2000;
			}
			break;
		case INVINCIBILITY:
			if(player.powerupcountdown > 0)
			{
				player.powerupcountdown--;
				player.health = 50;
			}
			else
			{
				player.powerup = NOPOWER;
				player.powerupcountdown = 2000;
			}
			break;
		case HEALTHBOOST:
			player.health = 50;
			player.powerup = NOPOWER;
			break;
		case NOPOWER:
			break;
	}
}
void drawpowerupnote(ALLEGRO_FONT *font, Player &player)
{
	switch(player.powerup)
	{
		case UNLIMAMMO:
			al_draw_text(font,al_map_rgb(255,255,255),HEIGHT,30,0,"UNLIMITED AMMO");
			break;
		case INVINCIBILITY:
			al_draw_text(font,al_map_rgb(255,255,255),HEIGHT,30,0,"INVINCIBILITY");
			break;
		case HEALTHBOOST:
			break;
		case NOPOWER:
			break;
	}
}

//Collision////////////////////////////
void PowerupandPlayer(Player &player,Powerups power[])
{
	for(int i = 0; i < 5; i++)
	{
		if(power[i].live)
		{
			float distance = sqrt(pow(float(player.x - power[i].x), 2) + pow(float(player.y  - power[i].y), 2));
			if(distance < 30 && player.powerup == NOPOWER)
			{
				power[i].live = false;
				player.powerup = power[i].type;
				break;
			}
		}
	}
}
void BulletandEvilrobots(Bullet bullet[], Evilrobots evilrobots[], Player player)
{
        for(int i = 0; i < 100; i++)
        {
                if(bullet[i].live)
                {
                        for(int c = 0; c < 100; c++)
                        {
                                if(evilrobots[c].live)
                                {
									float distance = sqrt(pow(float(bullet[i].x - evilrobots[c].x), 2) + pow(float(bullet[i].y  - evilrobots[c].y), 2));
                                        if(distance < 20)
                                        {
                                                evilrobots[c].live = false;
                                                bullet[i].live = false;
												player.score = player.score+5;
                                        }
                                }
                        }
                }
        }
}
void FrisbeeandPlayer(Player &player,Frisbee frisbee[])
{
        for(int i = 0; i < 5; i++)
        {
                if(frisbee[i].live)
                {
                        float distance = sqrt(pow(float(player.x - frisbee[i].x), 2) + pow(float(player.y  - frisbee[i].y), 2));
                        if(distance < 20)
                        {
                                player.score = player.score+10;
                                frisbee[i].live = false;
                        }
                }
        }
}
void PlayerandEvilrobots(Evilrobots evilrobots[], Player &player)
{
        for(int i = 0; i < 100; i++)
        {
                if(evilrobots[i].live)
                {
                        float distance = sqrt(pow(float(player.x - evilrobots[i].x), 2) + pow(float(player.y  - evilrobots[i].y), 2));
                        if(distance < 32)
                        {
							float testdx = evilrobots[i].x - player.x;
							float testdy = evilrobots[i].y - player.y;

							float vectorLength = sqrt(testdx * testdx + testdy * testdy);
							float dx = testdx / vectorLength;
							float dy = testdy / vectorLength;

							evilrobots[i].x = evilrobots[i].x + dx*2;
							evilrobots[i].y = evilrobots[i].y + dy*2;

							player.x = player.x - dx*2;
							player.y = player.y - dy*2;
                        }
						distance = sqrt(pow(float(player.x - evilrobots[i].x), 2) + pow(float(player.y  - evilrobots[i].y), 2));
						if(distance < 45)
							player.health = player.health - 0.09;
						if(distance > 50 && player.health < 50)
							player.health = player.health + 0.001;
                }
        }
}